package br.com.sovi.mobile.j2me.stream;

import br.com.sovi.mobile.j2me.SoviMidlet;
import br.com.sovi.mobile.j2me.gps.LocationRecord.Point;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.control.ToneControl;

/**
 *
 * @author Joao Galli
 */
public class RecordPointStream extends AbstractStream {

    private String baseURL;
    private String id;
    private Vector points;

    public RecordPointStream(StreamListener listener, String baseURL, String id, Vector points) {
        super(listener);
        this.baseURL = baseURL;
        this.id = id;
        this.points = points;
    }

    public RecordPointStream(StreamListener listener, String baseURL, String id, double longitude, double latitude) {
        super(listener);
        this.baseURL = baseURL;
        this.id = id;
        points = new Vector();
        points.addElement(new Point(new Date(), latitude, longitude));
    }

    public String getServletURL() {
        StringBuffer sb = new StringBuffer();

        if (baseURL == null || baseURL.equals("")) {
            throw new IllegalArgumentException("BaseUrl não foi preenchida.");
        }

        sb.append(baseURL);

        System.out.println("URL: " + sb.toString());

        return sb.toString();
    }

    protected void buildOutputStream(OutputStream hdout) throws Exception {
        StringBuffer sb = new StringBuffer();

        String formattedDate = formatDate(new Date());

        sb.append("d=").append(id);
        //sb.append("&data=").append(formattedDate);

        for (int i = 0; i < points.size(); i++) {
            Point point = (Point) points.elementAt(i);
            sb.append("&ps=").append(formattedDate).append(",").append(point.getLatitude()).append(",").append(point.getLongitude());
        }

        System.out.println("Conteúdo do Stream: " + sb.toString());

        byte[] bytes = sb.toString().getBytes();

        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            hdout.write(b);
        }

    }

    protected void parseInputStream(InputStream hdin) throws IOException {
        StringBuffer sb = new StringBuffer();
        int b = -1;

        while ((b = hdin.read()) > -1) {
            sb.append((char) b);
        }

        String response = sb.toString();

        if ("true".equals(response)) {
            SoviMidlet.get().getDisplay().flashBacklight(1000);
        } else if ("false".equals(response)) {
            SoviMidlet.get().getDisplay().vibrate(1000);
        }
    }

    private String formatDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        StringBuffer sb = new StringBuffer();
        sb.append(numberFormat(cal.get(Calendar.HOUR_OF_DAY)));
        sb.append(numberFormat(cal.get(Calendar.MINUTE)));
        sb.append(numberFormat(cal.get(Calendar.SECOND)));
        sb.append(numberFormat(cal.get(Calendar.DAY_OF_MONTH)));
        sb.append(numberFormat(cal.get(Calendar.MONTH)));
        sb.append(cal.get(Calendar.YEAR));

        return sb.toString();
    }

    public String numberFormat(int value) {
        if (value > 9) {
            return Integer.toString(value);
        } else {
            return "0" + value;
        }
    }
}
